Board = {};
// REQUIRES: error.js

/* PIECES:
 * e = empty
 * n = neutral
 * a = player a
 * b = player b
 * 
 * GOALS:
 * a = player a
 * b = player b
 * x = not a goal
 */

Board.createArray = function() {
	var arr = new Array(5);
	for (i = 0; i < arr.length; i++) {
		arr[i] = new Array(5);
	}
	return arr;
}

Board.setupTiles = function() {
	var arr = Board.createArray(); 
	
	for (i = 0; i < arr.length; i++) {
		for (j = 0; j < arr[i].length; j++) {
			if (i == 0) {
				arr[i][j] = 'b';
			}
			else if (i == 4) {
				arr[i][j] = 'a';
			}
			else {
				arr[i][j] = 'e';
			}
		}
	}
	
	return arr;
}

Board.setupGoals = function() {
	var goals = Board.createArray();
	
	for (i = 0; i < goals.length; i++) {
		for (j = 0; j < goals[i].length; j++) {
			if (i == 0) {
				goals[i][j] = 'a';
			}
			else if (i == 4) {
				goals[i][j] = 'b';
			}
			else {
				goals[i][j] = 'x';
			}
		}
	}
	
	return goals;
}

Board.isLegalMoveStart = function(player, tiles, r, c) {
	var piece = tiles[r][c];
	
	switch (piece) {
		case 'e':
			return false;
			break;
		case 'n':
			return true;
			break;
		case 'a':
			if (player == 'a') {
				return true;
			}
			break;
		case 'b':
			if (player == 'b') {
				return true;
			}
			break;
		default:
			Error.show('Board.isLegalMoveStart', 'unrecognized piece: ' + piece);
	}
	
	return false;
}

Board.isVictory = function(tiles, goals) {
	var badPieceA = false;
	var badPieceB = false;
	
	for (i = 0; i < goals.length; i++) {
		for (j = 0; j < goals[i].length; j++) {
			if (goals[i][j] == 'a' && tiles[i][j] != 'a') {
				badPieceA = true;
			}
			else if (goals[i][j] == 'b' && tiles[i][j] != 'b') {
				badPieceB = true;
			}
		}
	}
	
	if (!badPieceA || !badPieceB) return true;
	else return false;
}

Board.setupNoLegalMoves = function() {
	var moves = Board.createArray();
	
	for (i = 0; i < moves.length; i++) {
		for (j = 0; j < moves[i].length; j++) {
			moves[i][j] = false;
		}
	}
	
	return moves;
}

Board.setupLegalMoves = function(tiles, r, c) {
	var moves = Board.createArray();	
	
	for (i = 0; i < tiles.length; i++) {
		for (j = 0; j < tiles[i].length; j++) {
			if (Board.isLegalMoveEnd(tiles, r, c, i, j)) {
				moves[i][j] = true;
			}
			else {
				moves[i][j] = false;
			}
		}
	}
	
	return moves;
}

Board.isLegalMoveEnd = function(tiles, r1, c1, r2, c2) {
	return(
		Board.isEmpty(tiles, r2, c2) &&
		Board.isAdjacent(r1, c1, r2, c2));
}

Board.makeMove = function(tiles, r1, c1, r2, c2) {
	var piece = tiles[r1][c1]; 
	tiles[r1][c1] = 'e';
	tiles[r2][c2] = piece;
	
	return tiles;
}
	
Board.isEmpty = function(tiles, r, c) {
	var space = tiles[r][c];
	
	switch (space) {
		case 'e':
			return true;
		default:
			return false;
	}
}
	
Board.isAdjacent = function(r1, c1, r2, c2) {
	if (Math.abs(r1 - r2) == 1) {
		if (c1 == c2) {
			return true;
		}
	}
	else if (Math.abs(c1 - c2) == 1) {
		if (r1 == r2) {
			return true;
		}
	}
	
	return false;
}